<?php
include_once '../common_mbs.inc.php';
include_once '../smarty.inc.php';

//判断访问权限
$admin_index_module_obj = new osgh_admin_index_module_cls();
$is_warrant = $admin_index_module_obj->is_have_warrant($_ADMIN_ID,'company_list');
if(!$is_warrant) tcmc_cls::ShowMsg("没有权限访问！", "../index_body.html");

$company_obj = new company_cls();
$act = trim($_INPUT['action']);
$company_id  = $_INPUT['company_id'];
if($act =='save')
{	
	$data = array(
			'company_name'   => $_INPUT['company_name'],
			'company_url' => $_INPUT['company_url'],		
			'company_logo' => $_INPUT['company_logo']		
		);
	$path = 'E:/wamp/www/my-qy-project/upload/company/'.$_INPUT['company_logo'];
	if(file_exists($path))
	{		
		PresizeImg($path,82,32);
	}
	if(empty($company_id)){		
		$data['add_time'] = time();
		$company_obj->add_company($data);
		tcmc_cls::ShowMsg("添加企业成功！", "company_list.php");
	}else{		  
		   $company_obj->update_company($data, $company_id);
		   tcmc_cls::ShowMsg("修改企业成功！", "company_list.php");
	}
}elseif($act =='del'){
	$company_obj->del_company($company_id);
	tcmc_cls::ShowMsg("删除企业成功！", "company_list.php");
}

function PresizeImg($srcName,$newWidth,$newHeight)
{     
	$nameArr    = explode('.',$srcName);
	$src_Name   = basename($srcName, '.'.$nameArr[1]);
	$nameArr[0] = $src_Name;
	$newName    = $nameArr[0].'.'.$nameArr[1];
	$src        = substr($srcName, 0 ,strrpos($srcName,'/')).'/';
	$new_src    = $src.$newName;
	switch ($nameArr[1]){
		case jpg  :
		case JPG  :
		case jpeg :
		case JPEG :
			 if(!function_exists("imagecreatefromjpeg")){
				return "你的GD库不能使用jpeg格式的图片，请使用其它格式的图片！";
				exit();
			 }
			$im = ImageCreateFromJpeg($srcName);
		break;
		case png :
		case PNG :
			$im = ImageCreateFromPNG($srcName);
		break;
		case gif :
		case GIF :
			if(!function_exists("imagecreatefromgif")){
				return "你的GD库不能使用GIF格式的图片，请使用Jpeg或PNG格式！";
				exit();
			}
			$im = ImageCreateFromGIF($srcName);
		break;
		default:
			return '图片格式错误！！！';
	}
	$srcW=imagesx($im);
	$srcH=imagesy($im);	
	$ni = imagecreatetruecolor($newWidth,$newHeight);
	
	imagecopyresampled($ni,$im,0,0,0,0,$newWidth,$newHeight,$srcW,$srcH);
	imagejpeg($ni,$new_src);
	switch ($nameArr[1]){
		case jpg  :
		case JPG  :
		case jpeg :
		case JPEG :
			  imagejpeg($ni,$new_src);
		break;
		case png :
		case PNG :
		   imagepng($ni,$new_src);
		break;
		case gif :
		case GIF :
			imagegif($ni,$new_src);
		break;
   }
  
   ImageDestroy($ni);
}
?>
